package com.erp.module.pieceworkGrouping

import android.annotation.SuppressLint
import android.content.Context
import android.os.AsyncTask
import android.util.Log
import android.widget.ArrayAdapter
import com.erp.Interface.OnUserDataInterface
import com.erp.R
import com.erp.contacts.Contacts
import com.erp.mclass.UnCeHandler
import com.erp.util.Httputil
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.activity_baomingxi.spGuihao
import org.xutils.http.RequestParams
import org.xutils.x
import java.io.BufferedReader
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.URL


class PieceworkGroupingM {
    //获取小组数据
    fun getPiecework(context: Context, kgg_id: String, onUserDataInterface: OnUserDataInterface) {
        val params = RequestParams(Contacts.GET_PIECEWORK + "/" + kgg_id)
        Httputil.getparams(context, params)
        x.http().get<String>(params, Httputil.Call(context, onUserDataInterface))
    }

    //上传扫描数据
    fun postPieceworkcoll(context: Context, map: Map<String, String>, onUserDataInterface: OnUserDataInterface) {
        val params = RequestParams(Contacts.POST_PIECEWORKCOLL)
        Httputil.getparams(context, params)
        params.addBodyParameter("gangwei_id", map["gangwei_id"], Contacts.BOUNDARY)
        params.addBodyParameter("kgg_id", map["kgg_id"], Contacts.BOUNDARY)
        params.addBodyParameter("gangwei_name", map["gangwei_name"], Contacts.BOUNDARY)
        params.addBodyParameter("xm", map["xm"], Contacts.BOUNDARY)
        params.addBodyParameter("unit_price", map["unit_price"], Contacts.BOUNDARY)
        params.addBodyParameter("worker_group", map["worker_group"], Contacts.BOUNDARY)
        params.addBodyParameter("qr_content", map["qr_content"], Contacts.BOUNDARY)
        params.addHeader("ACCEPT", "application/json")
        x.http().post<String>(params, Httputil.Call(context, onUserDataInterface))
    }

    fun getPieceworkcoll(context: Context, page: Int, pagesize: Int, onUserDataInterface: OnUserDataInterface) {
        val params = RequestParams(Contacts.GET_PIECEWORKCOLL)
        Httputil.getparams(context, params)
        params.addBodyParameter("page", page.toString())
        params.addBodyParameter("page_size", pagesize.toString())
        x.http().get<String>(params, Httputil.Call(context, onUserDataInterface))
    }

    fun getGroupingNames(context: Context, onUserDataInterface: OnUserDataInterface) {
        val params = RequestParams(Contacts.GET_GROUPING_NAMES)
        Httputil.getparams(context, params)
        x.http().get<String>(params, Httputil.Call(context, onUserDataInterface))
    }

    fun getGroupingList(context: Context, map: Map<String, String>, onUserDataInterface: OnUserDataInterface) {
        val params = RequestParams(Contacts.GET_GROUPING_LIST)
        params.addBodyParameter("gangwei_name", map["gangwei_name"])
        params.addBodyParameter("name", map["name"])
        params.addBodyParameter("staff_name", map["staff_name"])
        Httputil.getparams(context, params)
        x.http().get<String>(params, Httputil.Call(context, onUserDataInterface))
    }

    fun getGroupingHZList(context: Context, page: Int, starttime: String, endtime: String, code: Int, batch_names :String, onUserDataInterface: OnUserDataInterface) {
        val params = RequestParams(Contacts.GET_GROUPING_HZ)
        params.addBodyParameter("page", page.toString())
        if (code == 1) {
            params.addBodyParameter("hz", "")
        } else if (code == 2) {
            params.addBodyParameter("hz", "按日期汇总")
        } else if (code == 3) {
            params.addBodyParameter("hz", "按月份汇总")
        }
        if (starttime != "" && endtime != "") {
            params.addBodyParameter("sp[pcoll_at_range][0]", starttime)
            params.addBodyParameter("sp[pcoll_at_range][1]", endtime)
        }
        if(batch_names != ""){
            params.addBodyParameter("sp[batch_names][0]", batch_names)
        }
        Httputil.getparams(context, params)
        x.http().get<String>(params, Httputil.Call(context, onUserDataInterface))
    }

    fun caveScancode(context: Context, scan: String, sign: String, onUserDataInterface: OnUserDataInterface) {
        val params = RequestParams(Contacts.SAVE_SCANCODE)
        params.addBodyParameter("scan_code", scan)
        params.addBodyParameter("sign", sign)
        Httputil.getparams(context, params)
        x.http().post<String>(params, Httputil.Call(context, onUserDataInterface))
    }
}